package cn.lg.soar.core.sn;

import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public interface SnElement {

	/**
	 * 获取元素key（确保唯一性）
	 * @return
	 */
	String getKey();

	/**
	 * 获取元素值
	 * @param args
	 * @return
	 */
	String getValue(Object...args);

	@Component
	class Year implements SnElement {
		@Override
		public String getKey() {
			return "year";
		}
		@Override
		public String getValue(Object... args) {
			return String.valueOf(LocalDate.now().getYear());
		}
	}

	@Component
	class Month implements SnElement {
		@Override
		public String getKey() {
			return "month";
		}
		@Override
		public String getValue(Object... args) {
			int month = LocalDate.now().getMonthValue();
			return month < 10 ? ("0" + month) : String.valueOf(month);
		}
	}

	@Component
	class Day implements SnElement {
		@Override
		public String getKey() {
			return "day";
		}
		@Override
		public String getValue(Object... args) {
			int day = LocalDate.now().getDayOfMonth();
			return day < 10 ? ("0" + day) : String.valueOf(day);
		}
	}

	@Component
	class Date implements SnElement {
		private final static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
		@Override
		public String getKey() {
			return "date";
		}
		@Override
		public String getValue(Object... args) {
			return formatter.format(LocalDate.now());
		}
	}

	@Component
	class Datetime implements SnElement {
		private final static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
		@Override
		public String getKey() {
			return "datetime";
		}
		@Override
		public String getValue(Object... args) {
			if (args.length == 0) {
				return formatter.format(LocalDateTime.now());
			} else {
				return DateTimeFormatter.ofPattern((String) args[0]).format(LocalDateTime.now());
			}
		}
	}

	@Component
	class Random implements SnElement {
		private final static java.util.Random random = new java.util.Random();
		@Override
		public String getKey() {
			return "random";
		}
		@Override
		public String getValue(Object... args) {
			int length = (int)args[0];
			String str;
			if (args.length == 2) {
				str = (String) args[1];
			} else {
				str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
			}
			char[] source = str.toCharArray();
			char[] chars = new char[length];
			for (int i = 0; i < length; i++) {
				chars[i] = source[random.nextInt(10)];
			}
			return String.valueOf(chars);
		}
	}

	@Component
	class RandomNumber implements SnElement {
		@Override
		public String getKey() {
			return "randomNumber";
		}
		@Override
		public String getValue(Object... args) {
			int length = (int)args[0];
			String s = String.valueOf(Math.random());
			return s.substring(2, 2 + length);
		}
	}

	@Component
	class Const implements SnElement {
		@Override
		public String getKey() {
			return "const";
		}
		@Override
		public String getValue(Object... args) {
			return (String) args[0];
		}
	}

	@Component
	class Args implements SnElement {
		@Override
		public String getKey() {
			return "args";
		}
		@Override
		public String getValue(Object... args) {
			return (String) args[0];
		}
	}

}
